home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / src.zoo / src / down_load.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-20  |  11.1 KB  |  321 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: down_load.c,v 1.2 89/03/20 13:14:07 sau Exp $
  9.     $Source: /m1/mgr.new/src/RCS/down_load.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /m1/mgr.new/src/RCS/down_load.c,v $$Revision: 1.2 $";
  12.  
  13. /* down load text -- called from put_window.c */
  14.  
  15. #include <sys/file.h>
  16. #include "bitmap.h"
  17. #include "font.h"
  18. #include "defs.h"
  19. #include "menu.h"
  20. #include "event.h"
  21. #include <stdio.h>
  22. #include "window.h"
  23.  
  24. int
  25. down_load(win,window,text)
  26. register WINDOW *win;
  27. BITMAP *window, *text;
  28.    {
  29.    int cnt = W(esc_cnt);
  30.    struct font *Get_font();
  31.    struct menu_state *do_menu();
  32.    int id;
  33.  
  34.       switch(W(code)) {
  35.          case T_INVALID:        /* invalid text mode */
  36.         if( debug )
  37.            fprintf(stderr,"mgr: invalid download code\n");
  38.         break;
  39.          case T_MENU:                /* down load menu */
  40.                 {
  41.                 struct font *f;
  42.                 int fn = W(esc[1]);
  43.  
  44.                 if (*W(snarf) && cnt>1 ) {
  45.                    f=Get_font(fn);
  46.                    }
  47.                 else
  48.                    f = W(font);
  49.                   
  50.                 if (*W(snarf)) {
  51.                    W(menus)[*W(esc)] = do_menu(W(snarf),f,
  52.                            PUTCOLOR(W(style)),PUTCOLOR(W(background)));
  53.            if( active == win ) {
  54.                       if (W(menu[0]) == *W(esc) && button_state==BUTTON_2)
  55.                         go_menu(0);
  56.                       else if (W(menu[1]) == *W(esc) && button_state==BUTTON_1)
  57.                         go_menu(1);
  58.               }
  59.                    }
  60.                 else
  61.                    W(menus)[*W(esc)] = (struct menu_state *) 0;
  62.                 free(W(snarf));
  63.                 W(snarf) = (char *) 0;
  64.                 }
  65.                 break;
  66.          case T_EVENT:                /* down load an event */
  67.                 cnt=W(esc)[0];
  68.                 if (!CHK_EVENT(cnt)) {
  69.                    free(W(snarf));
  70.                    W(snarf) = (char *) 0;
  71.                    break;
  72.                    }
  73.                 if (W(events)[GET_EVENT(cnt)]) {
  74.                    free (W(events)[GET_EVENT(cnt)]);
  75.                    W(events)[GET_EVENT(cnt)] = (char *) 0;
  76.                    }
  77.                 if (*W(snarf)) {
  78.                    W(events)[GET_EVENT(cnt)] = W(snarf);
  79.                    EVENT_SET_MASK(win,cnt);
  80. #ifdef DEBUG
  81.                    dprintf(e)(stderr,"%s: setting event %d (%d)[%s]\r\n",
  82.                            W(tty),GET_EVENT(cnt),strlen(W(snarf)), W(snarf));
  83. #endif
  84.                    /* if button is down, then do the associated event */
  85.  
  86.                    if (win == active  &&
  87.             (cnt==EVENT_B1_DOWN && button_state == BUTTON_1 ||
  88.                          cnt==EVENT_B2_DOWN && button_state == BUTTON_2))
  89.               do_event(button_state,win,E_MAIN);
  90.                    }
  91.                 else  {
  92.                    EVENT_CLEAR_MASK(win,cnt);
  93. #ifdef DEBUG
  94.                    dprintf(e)(stderr,"%s: clearing event %d\r\n",
  95.                              W(tty),GET_EVENT(cnt));
  96. #endif
  97.                    free(W(snarf));
  98.                    }
  99.                 W(snarf) = (char *) 0;
  100.                 break;
  101.          case T_STRING:                /* draw text into offscreen bitmap */
  102.                 {
  103.                 int x = cnt>1 ? Scalex(W(esc[1])) : 0;
  104.                 int y = cnt>2 ? Scaley(W(esc[2])) : 0;
  105.  
  106.                 if (y<FSIZE(high)) y=FSIZE(high);
  107.                 if (x<0) x=0;
  108. #ifdef DEBUG
  109.                 dprintf(y)(stderr,"%s: drawing [%s] to %d\r\n",
  110.                     W(tty),W(snarf),*W(esc));
  111. #endif
  112.                 if (*W(esc)>0 && W(bitmaps)[*W(esc)-1] == (BITMAP *) 0) {
  113.                    W(bitmaps)[*W(esc)-1] = bit_alloc(
  114.                           x+strlen(W(snarf))*FSIZE(wide),
  115.                           y, NULL_DATA,DEPTH);
  116. #ifdef DEBUG
  117.                 dprintf(y)(stderr,"%s: STRING creating %d (%dx%d)\n",
  118.                           W(tty),*W(esc),
  119.                           x+strlen(W(snarf))*FSIZE(wide),y);
  120. #endif
  121.                    }
  122.                 if (*W(esc) > 0)
  123.                    put_str(W(bitmaps)[*W(esc)-1],x,y,
  124.                            W(font),W(op),W(snarf));
  125.                 else
  126.                    put_str(window,x,y,W(font),W(op),W(snarf));
  127.                 }
  128.                 break;
  129.          case T_YANK:                /* fill yank buffer */
  130.                 if (snarf)
  131.                    free (snarf);
  132.                 snarf = W(snarf);
  133. #ifdef DEBUG
  134.                 dprintf(y)(stderr,"%s: yanking [%s]\r\n",W(tty),snarf);
  135. #endif
  136.                 id_message = W(pid);
  137.                 W(snarf) = (char *) 0;
  138.                 for(win=active;win != (WINDOW *) 0;win=W(next))
  139.                    do_event(EVENT_SNARFED,win,E_MAIN);
  140.                 break;
  141.  
  142.          case T_SEND:                /* send a message */
  143.                 id = *W(esc);
  144.                 if (message) {
  145.                    free(message);
  146.                    message = (char *) 0;
  147.                    }
  148.                 message = W(snarf);
  149.                 id_message = W(pid);
  150.                 W(snarf) = (char *) 0;
  151. #ifdef DEBUG
  152.                 dprintf(e)(stderr,"%s: sending [%s]\r\n",W(tty),W(snarf));
  153.                 dprintf(c)(stderr,"sending %d->%d: %s\r\n",
  154.                    W(pid),cnt==0?0:id,message);
  155. #endif
  156.                 for(win=active;win != (WINDOW *) 0;win=W(next))
  157.                    if (cnt==0 || W(pid)==id) {
  158.                       do_event(EVENT_ACCEPT,win,E_MAIN);
  159.                       if (cnt) break;
  160.                       }
  161.                 break;
  162.  
  163.          case T_GMAP:                /* load a bitmap from a file */
  164.                 {
  165.                    BITMAP *b, *bitmapread();
  166.            FILE *fp = NULL;
  167.                    char filename[MAX_PATH];
  168.                    char buff[20];
  169.                    char c = *W(snarf);
  170.  
  171.                    /* make relative to icon directory */
  172.  
  173.                    if (c == '/' || (c == '.' && W(snarf)[1]=='/'))
  174.                       strcpy(filename,W(snarf));
  175.                    else
  176.                       sprintf(filename,"%s/%s",icon_dir,W(snarf));
  177.  
  178.                if (W(flags)&W_DUPKEY)
  179.               sprintf(buff,"%c ",W(dup));
  180.                    else
  181.                       *buff = '\0';
  182.  
  183.                    if (*W(esc) > 0 && *W(esc) < MAXBITMAPS &&
  184.                             read_ok(filename) &&
  185.                 (fp = fopen(filename,"r")) != NULL &&
  186.                             (b = bitmapread(fp))) {
  187.               if( W(bitmaps[*W(esc)-1]) )
  188.              bit_destroy( W(bitmaps[*W(esc)-1]) );
  189.               W(bitmaps[*W(esc)-1]) = b;
  190.                       sprintf(buff+strlen(buff),"%d %d\n",
  191.                                   BIT_WIDE(b),BIT_HIGH(b));
  192.                       }
  193.                    else {
  194.                       strcat(buff,"\n");
  195.                       }
  196.                    write(W(to_fd),buff,strlen(buff));
  197.  
  198.            if( fp != NULL )
  199.             fclose(fp);
  200.                    }
  201.                 break;
  202.  
  203.          case T_SMAP:                /* save a bitmap on a file */
  204.                 {
  205.            FILE *fp;
  206.                    BITMAP *b;
  207.                    int size;
  208.                    int exists;        /* file already exists */
  209.                    int free_b = 0;
  210.                    int num = *W(esc);
  211.                    BITMAP *get_map();
  212.  
  213.                    switch(cnt) {
  214.                       case 1:            /* off screen bitmap */
  215.                          if (num > 0)
  216.                             b = W(bitmaps[num-1]);
  217.                          else
  218.                             b = screen;
  219.                          break;
  220.                       case 0:            /* my window */
  221.                          free_b++;
  222.                          b = bit_alloc(BIT_WIDE(window),BIT_HIGH(window),
  223.                                        NULL_DATA,DEPTH);
  224.                          if (b)
  225.                             bit_blit(b,0,0,BIT_WIDE(b),BIT_HIGH(b),BIT_SRC,window,0,0);
  226.                          break;
  227.                       case 2:            /* other guy's window */
  228.                          free_b++;
  229.                          b = get_map(num,W(esc[1]));
  230.                          break;
  231.                       }
  232.                
  233. #ifdef DEBUG
  234.                    dprintf(*)(stderr,"saving...\n");
  235. #endif
  236.                    if (b && W(snarf) && ((exists=access(W(snarf),0)),
  237.                             write_ok(W(snarf))) &&
  238.                             (fp = fopen(W(snarf),"w")) != NULL) {
  239. #ifdef DEBUG
  240.                       dprintf(y)(stderr,"saving bitmap %d x %d on %s (%d)\n",BIT_WIDE(b),BIT_HIGH(b),W(snarf),fileno(fp));
  241. #endif
  242.                       if (exists<0)    /* file just created */
  243.                          fchown(fileno(fp),getuid(),getgid());
  244.                       bitmapwrite( fp, b, get_bm_type() );
  245.                       fclose(fp);
  246. #ifdef DEBUG
  247.                       dprintf(y)(stderr,"saved %d on %s\n",size,W(snarf));
  248. #endif
  249.                       }
  250.                    if (W(snarf))
  251.                       free(W(snarf));
  252.                    W(snarf) = (char *) 0;
  253.  
  254.                    if (b && free_b)
  255.                       bit_destroy(b);
  256.                    }
  257.                 break;
  258.  
  259.          case T_GIMME:                /* send to process */
  260.                 if (W(snarf) && *W(snarf)) 
  261.                    Write(W(to_fd),W(snarf),strlen(W(snarf)));
  262. #ifdef DEBUG
  263.                 dprintf(y)(stderr,"%s: sending [%s]\r\n",W(snarf));
  264. #endif
  265.                 if (W(snarf))
  266.                    free(W(snarf));
  267.                 W(snarf) = (char *) 0;
  268.                 break;
  269.  
  270.          case T_GRUNCH:                /* graphics scrunch mode (experimental) */
  271.                 if (W(snarf)) 
  272.                    grunch(win,window);
  273. #ifdef DEBUG
  274.                 dprintf(y)(stderr,"%s: grunching [%d]\r\n",W(tty),W(esc)[cnt]);
  275. #endif
  276.                 if (W(snarf))
  277.                    free(W(snarf));
  278.                 W(snarf) = (char *) 0;
  279.                 break;
  280.  
  281.          case T_FONT:                /* change a font name */
  282.                 if (W(esc)[0] <= MAXFONT && W(esc)[0] > 0) {
  283.                    if (fontlist[W(esc[0])-1])
  284.                       free(fontlist[W(esc[0])-1]);
  285.                    fontlist[W(esc[0])-1] = W(snarf);
  286.                    }
  287.                 else
  288.                    free(W(snarf));
  289.                 W(snarf) = (char *) 0;
  290.                 break;
  291.          case T_BITMAP:                /* down-load a bitmap */
  292.                 win_map(win,window);
  293.                 break;
  294.          }
  295.    }
  296.  
  297. /* find bitmap associated with window id */
  298.  
  299. BITMAP *
  300. get_map(id,sub)
  301. int id;                /* pid of process controlling window */
  302. int sub;            /* window number of this window */
  303.    {
  304.    register WINDOW *win;
  305.    register BITMAP *map;
  306.  
  307.    for(win=active;win != (WINDOW *) 0;win=W(next))
  308.       if (W(pid)==id && W(num)==sub) {
  309.          map = bit_alloc(BIT_WIDE(W(window)),BIT_HIGH(W(window)),
  310.                          NULL_DATA,DEPTH);
  311.          if (map && W(flags)&W_ACTIVE)
  312.             bit_blit(map,0,0,BIT_WIDE(map),BIT_HIGH(map),
  313.                      BIT_SRC,W(window),0,0);
  314.          else if (map)
  315.             bit_blit(map,0,0,BIT_WIDE(map),BIT_HIGH(map),
  316.                      BIT_SRC,W(save),SUM_BDR,SUM_BDR);
  317.          return(map);
  318.          }
  319.    return(BIT_NULL);
  320.    }
  321.